package com.mobile.check.activitys;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.base.BaseListActivity;
import cn.droidlover.xdroidmvp.base.BaseRecyclerAdapter;
import cn.droidlover.xdroidmvp.base.RecyclerViewHolder;
import com.mobile.check.beans.TaskInfo;
import com.mobile.check.beans.TaskItemInfo;
import com.mobile.check.app.R;
import com.mobile.check.present.TaskDetailPresent;
import com.mobile.check.views.TipDialog;

/**
 * 类名: TaskDetailActivity
 * 作者: 李学伟
 * 日期: 2020年10月09日 13:39
 * 描述:
 */
public class TaskItemListActivity extends BaseListActivity<TaskItemInfo, TaskDetailPresent> {
    TaskInfo taskInfo;
    Boolean isRepeat;
    @BindView(R.id.totalNum)
    TextView totalNumTv;
    @BindView(R.id.testNum)
    TextView testNumTv;
    @BindView(R.id.falNum)
    TextView falNumTv;
    @BindView(R.id.inputEt)
    EditText inputEt;
    @BindView(R.id.clearIv)
    ImageView clearIv;

    Handler mhandler;

    @Override
    public void initView() {
        super.initView();
        titleTv.setText("检测项目");
        rightTv.setText("完成");
        rightTv.setVisibility(View.VISIBLE);

        inputEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String text = inputEt.getText().toString();
                if (TextUtils.isEmpty(text)) {
                    clearIv.setVisibility(View.GONE);
                } else {
                    clearIv.setVisibility(View.VISIBLE);
                }
                getP().getTaskItems(taskInfo, text);
            }
        });

        clearIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inputEt.setText("");
            }
        });

        mhandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                String message = msg.obj.toString();
                if (getP().isAllowExport()) {
                    showTipDialog(message,"停止导出",false);
                }
            }
        };


    }


    @Override
    public int getContentViewId() {
        return R.layout.activity_taskdetail;
    }

    @Override
    public BaseRecyclerAdapter getAdapter() {
        return new BaseRecyclerAdapter<TaskItemInfo>(this) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.item_taskitem;
            }

            @Override
            public void bindData(RecyclerViewHolder holder, int position) {
                super.bindData(holder, position);
                TaskItemInfo info = getItemByPosition(position);
//                holder.setText(R.id.noTv, String.valueOf(position + 1) + "、");
                holder.setText(R.id.title, info.getLabel());
                holder.setText(R.id.typeTv, info.getAssemblyType());
                holder.setText(R.id.childTypeTv, info.getAssemblyChildType());
                if (info.isHadTest()) {
                    if (info.isQualified()) {
                        holder.setImageResDrawable(R.id.image, R.mipmap.status3);
                    } else {
                        holder.setImageResDrawable(R.id.image, R.mipmap.status1);
                    }
                } else {
                    holder.setImageResDrawable(R.id.image, R.mipmap.status2);
                }
            }
        };
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        getP().setHandler(mhandler);
    }

    @Override
    protected void onResume() {
        super.onResume();
        inputEt.setText("");
        getP().getTaskItems(taskInfo);
    }

    @Override
    public void itemClick(int position) {
        Intent intent = new Intent(this, TaskItemCheckActivity.class);
        intent.putExtra("index", position);
        intent.putExtra("filepath", taskInfo.getTaskFilePath());
        TaskItemInfo info = mBaseAdapter.getItemByPosition(position);
        TaskItemCheckActivity.lunch(this, info.getParentId(), position, taskInfo.getTaskType(), taskInfo.isLocalFile());
    }

    @Override
    public void getDataFromServer() {

    }

    @Override
    public void getIntentData() {
        taskInfo = getIntent().getParcelableExtra("taskInfo");
        isRepeat = getIntent().getBooleanExtra("isRepeat", false);
    }

    @Override
    public TaskDetailPresent newP() {
        return new TaskDetailPresent();
    }

    public static void lunch(Context context, boolean isRepeat, TaskInfo taskInfo) {
        Intent intent = new Intent(context, TaskItemListActivity.class);
        intent.putExtra("isRepeat", isRepeat);
        intent.putExtra("taskInfo", taskInfo);
        context.startActivity(intent);
    }

    public void parserSuccess(int testNum, int falNum, List<TaskItemInfo> list) {
        totalNumTv.setText(String.valueOf(list.size()));
        testNumTv.setText(String.valueOf(testNum));
        falNumTv.setText(String.valueOf(falNum));
        mBaseAdapter.addList(true, list);
        disLoadDialog();
    }

    @Override
    public boolean isNotWorkConnect() {
        return false;
    }

    @Override
    public boolean showNoDataPage() {
        return true;
    }

    public void onSuccess(boolean isRefresh, List<TaskItemInfo> list) {
        mBaseAdapter.addList(isRefresh, list);
    }

    @Override
    public void onRightClick(View view) {
        showLoadDialog();
        getP().startSaveToFile(taskInfo);
    }

    TipDialog tipDialog = null;

    public void showTipDialog(String msg, final String btnText, final boolean clickFinish) {
        disLoadDialog();
        if (tipDialog == null) {
            tipDialog = new TipDialog(this, new TipDialog.SlectClickListener() {
                @Override
                public void submitClick(boolean clickFinish) {
                    if (TextUtils.equals("停止导出",btnText)) {
                        getP().stopExport();
                    }else if(clickFinish){
                        finish();
                    }
                }

                @Override
                public void cancelClick() {

                }
            }, msg);
            tipDialog.display("导出", msg, "", btnText);
        } else {
            if (tipDialog.isShowing()) {
                tipDialog.setMessage(msg);
                tipDialog.setSubmit(btnText, clickFinish);
            }else{
                tipDialog.display("导出", msg, "", btnText);
            }
        }

    }
}
